package com.qk.controller;

import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.PageResult;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping()
    public Result list(UserDto userDto) {
        PageResult<User> pageResult = userService.list(userDto);
        return Result.success( pageResult);

    }
    @PostMapping
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        userService.delete(ids);
        return Result.success();

    }
    @PutMapping
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getByID(@PathVariable Integer id) {
        return Result.success(userService.getByID(id));
    }
    @GetMapping("/role/{roleLabel}")
    public Result getByRole(@PathVariable String roleLabel){
        return Result.success(userService.getByRole(roleLabel));
    }
    @GetMapping("/list")
    public Result getAll() {
        return Result.success(userService.getAll());
    }

}
